#!/bin/bash

if [ -z $1 ]; then
echo 'missing a parameter'
echo 'HELP:'
echo gen : sencha generate app $2 ../$2
echo prod : sencha app build production
echo package : sencha app build package
echo css : compass compile resources/sass/
echo watch : compass watch resources/sass/
echo android : sencha app package run android
echo apro : sencha app production run android
echo arel : sencha app production Release android
echo ios : sencha app package run ios
echo iospro : sencha app package run ios
echo genkey : create project release key
exit
fi

if [ $1 = gen ]; then
echo sencha generate app $2 $2 --sdk-path=/home/porterzhang/work/senchawork/touch-2.2.0
sencha --sdk-path=/home/porterzhang/work/senchawork/touch-2.2.0 generate app $2 $2
fi


if [ $1 = genkey ]; then
  echo create project release key
  if [ -z $2 ]; then
    echo Parameter does not exist！
  else 
    if [ ! -f youdao/$2.key ];then
     echo "Generage the keystore:"
     echo "keytool -genkeypair -dname 'cn=you dao, ou=xayoudao.com, o=youdao, c=CN' -alias xayoudao.com -keypass 1qazxsw2@yd -keystore "$2".key -storepass 1qazxsw2@yd -validity 18250"
     keytool -genkeypair -dname "cn=you dao, ou=xayoudao.com, o=youdao, c=CN" -alias xayoudao.com -keypass 1qazxsw2@yd -keystore youdao/$2.key -storepass 1qazxsw2@yd -validity 18250
    else 
     echo $2'.key is exist!' 
    fi
  fi
fi


if [ $1 = prod ]; then
echo sencha app build production
sencha app build production
fi

if [ $1 = package ]; then
echo sencha app build package
sencha app build package
fi

if [ $1 = css ]; then
echo compass compile resources/sass/
compass compile resources/sass/
fi

if [ $1 = watch ]; then
echo compass watch resources/sass/
compass watch resources/sass/
fi

if [ $1 = android ]; then
echo delete _android.buit and sencha app package
rm _android.built
 if [ -z $2 ]; then
     rm -rf build/${PWD##*/} 
 else
    rm -rf build/$2
 fi

sencha app build production
   if [ -z $2 ]; then
        ./build/${PWD##*/}/android/cordova/run
   else
       ./build/$2/android/cordova/run
   fi
fi

if [ $1 = apro ]; then
echo delete _android.buit and sencha app production
rm _android.built 
rm -Rf build/archive
 if [ -z $2 ]; then
    rm -rf build/${PWD##*/}/android 
    rm -rf build/production
 else
    rm -rf build/$2/android
    rm -rf build/$2/production
 fi

sencha app build production 
   if [ -z $2 ]; then
        ./build/${PWD##*/}/android/cordova/run
   else
       ./build/$2/android/cordova/run
   fi
fi

if [ $1 = arel ]; then
echo delete _android.buit and sencha app production and Release
rm _android.built
rm -Rf build/archive
rm -rf build/${PWD##*/}/android 
rm -rf build/production

sencha app build production 
  
./build/${PWD##*/}/android/cordova/release
rm  youdao/distribution/${PWD##*/}.apk
cp ./build/${PWD##*/}/android/bin/${PWD##*/}-release.apk ./youdao/distribution/${PWD##*/}.apk
   
fi


if [ $1 = ios ]; then
echo sencha app production

rm _ios.built
rm -Rf build/archive
rm -Rf build/temp
rm -rf build/${PWD##*/}/ios

sencha app build production

fi


if [ $1 = iospro ]; then
echo sencha app build production
rm _ios.built
 if [ -z $2 ]; then
    echo rm ${PWD##*/}
    rm -rf build/${PWD##*/}/ios
    rm -rf build/${PWD##*/}/production
 else
    echo rm $2
    rm -rf build/$2/ios
    rm -rf build/$2/production
 fi  
rm -Rf archive
 sencha app build production 
  if [ ! -z $3 ]; then
    if [ -z $2 ]; then
       ./build/${PWD##*/}/ios/cordova/distribution  $3
       rm  youdao/distribution/${PWD##*/}.ipa 
       cp ./build/${PWD##*/}/ios/build/${PWD##*/}.ipa ./youdao/distribution/${PWD##*/}.ipa
    else
       ./build/$2/ios/cordova/distribution $3
       rm  youdao/distribution/$2.ipa 
       cp ./build/$2/ios/build/$2.ipa ./youdao/distribution/$2.ipa
    fi
  fi
fi

echo success!

 say success! 
 ##&afplay ~/bin/notice.mp3 &
